import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { IBaseResponse } from '../pojo/i-base-response';
import { Observable } from 'rxjs';
import { IShipAddress, IShipAddressResponse, IDetailResponse } from '../pojo/system/i-ship-address';

/**
 * 发货地址管理模块 接口
 *
 * @export
 * @class ApiShipAddressService
 */
@Injectable({
    providedIn: 'root'
})
export class ApiShipAddressService {
    constructor(
        private http: HttpClient
    ) {}

    /**
     * 获取发货地址列表
     *
     * @returns {Observable<IBaseResponse>}
     * @memberof ApiShipAddressService
     */
    getAddressList(): Observable<IShipAddressResponse> {
        return this.http.get<IShipAddressResponse>('address/list');
    }

    /**
     * 更新地址
     *
     * @param {IShipAddress} entity
     * @returns {Observable<IBaseResponse>}
     * @memberof ApiShipAddressService
     */
    updateAddress(entity: IShipAddress): Observable<IBaseResponse> {

        return this.http.post<IBaseResponse>('address/save', { address: {...entity}})
    }

    /**
     * 获取发货地址详情
     * @param {number} id 
     */
    getAddressDetail(id: number): Observable<IDetailResponse> {
        const params = new HttpParams()
            .set('id', `${id}`)
        return this.http.get<IDetailResponse>('address/details', { params })
    }

    /**
     * 删除发货地址
     * @param {number} id 
     */
    deleteAddress(id: number): Observable<IBaseResponse> {
        return this.http.post<IBaseResponse>('address/delete', { id })
    }
}
